package com.xy.xylive.service.impl;

import java.util.Date;
import java.util.List;

import ch.qos.logback.core.util.Duration;
import cn.hutool.core.date.BetweenFormatter;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.xy.xylive.hstapi.HstApiUtil;
import org.apache.commons.lang3.time.DurationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xy.xylive.mapper.LiveMapper;
import com.xy.xylive.domain.Live;
import com.xy.xylive.service.ILiveService;

/**
 * 直播Service业务层处理
 * 
 * @author ghostsf
 * @date 2024-05-19
 */
@Service
public class LiveServiceImpl implements ILiveService 
{
    @Autowired
    private LiveMapper liveMapper;

    /**
     * 查询直播
     * 
     * @param id 直播主键
     * @return 直播
     */
    @Override
    public Live selectLiveById(Integer id)
    {
        return liveMapper.selectLiveById(id);
    }

    /**
     * 查询直播列表
     * 
     * @param live 直播
     * @return 直播
     */
    @Override
    public List<Live> selectLiveList(Live live)
    {
        List<Live> liveList = liveMapper.selectLiveList(live);
        for (Live l : liveList) {
            if(l.getDuration() != null && StrUtil.isBlank(l.getDurationText())){
                l.setDurationText(DateUtil.formatBetween(l.getDuration(), BetweenFormatter.Level.SECOND));
            }
        }
        return liveList;
    }

    /**
     * 新增直播
     * 
     * @param live 直播
     * @return 结果
     */
    @Override
    public int insertLive(Live live)
    {
        live.setCreateTime(new Date());
        return liveMapper.insertLive(live);
    }

    /**
     * 修改直播
     * 
     * @param live 直播
     * @return 结果
     */
    @Override
    public int updateLive(Live live)
    {
        live.setUpdateTime(new Date());
        return liveMapper.updateLive(live);
    }

    /**
     * 批量删除直播
     * 
     * @param ids 需要删除的直播主键
     * @return 结果
     */
    @Override
    public int deleteLiveByIds(Integer[] ids)
    {
        return liveMapper.deleteLiveByIds(ids);
    }

    /**
     * 删除直播信息
     * 
     * @param id 直播主键
     * @return 结果
     */
    @Override
    public int deleteLiveById(Integer id)
    {
        // 改为逻辑删除
        Live live = selectLiveById(id);
        live.setIsDelete(1);
        live.setUpdateTime(new Date());
        return updateLive(live);
    }

    @Override
    public List<Live> selectEndList() {
        return liveMapper.selectEndList();
    }

    @Override
    public boolean checkByUserIdAndHstMeetingId(String userId, String hstMeetingId) {
        int r = liveMapper.countByUserIdAndHstMeetingId(userId, hstMeetingId);
        return r > 0;
    }
}
